package com.Brewing.springboot.service.TpVideos;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author  庄磊
 * @date	2018年12月26日 上午10:24:40 
 * @version 1.0
 */

import java.util.Date;
import java.util.List;

import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.Brewing.springboot.common.PagedResult;
import com.Brewing.springboot.common.TimeAgoUtils;
import com.Brewing.springboot.entity.TpComments.TpComments;
import com.Brewing.springboot.entity.TpNotice.TpNotice;
import com.Brewing.springboot.entity.TpSearchRecords.TpSearchRecords;
import com.Brewing.springboot.entity.TpUsersLikeVideos.TpUsersLikeVideos;
import com.Brewing.springboot.entity.TpUsersLikeVideos.TpUsersLikeVideosExample;
import com.Brewing.springboot.entity.TpUsersLikeVideos.TpUsersLikeVideosExample.Criteria;
import com.Brewing.springboot.entity.TpVideos.TpVideos;
import com.Brewing.springboot.entity.vo.CommentsVO;
import com.Brewing.springboot.entity.vo.VideosVO;
import com.Brewing.springboot.mapper.TpCommentsMapper;
import com.Brewing.springboot.mapper.TpCommentsMapperCustom;
import com.Brewing.springboot.mapper.TpNoticeMapper;
import com.Brewing.springboot.mapper.TpSearchRecordsMapper;
import com.Brewing.springboot.mapper.TpUsersLikeVideosMapper;
import com.Brewing.springboot.mapper.TpUsersMapper;
import com.Brewing.springboot.mapper.TpVideosMapper;
import com.Brewing.springboot.mapper.TpVideosMapperCustom;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class VideoServiceImpl implements VideoService{
	@Autowired 
	private TpVideosMapper videosMapper;
	@Autowired
	private TpVideosMapperCustom videosMapperCustom;
	@Autowired
	private TpUsersMapper usersMapper;
	@Autowired
	private TpSearchRecordsMapper tpSearchRecordsMapper;
	@Autowired
	private TpCommentsMapper tpCommentsMapper;
	@Autowired
	private TpCommentsMapperCustom tpCommentsMapperCustom;
	@Autowired
	private TpUsersLikeVideosMapper tpUsersLikeVideosMapper;
	@Autowired
	private TpNoticeMapper tpNoticeMapper;
	@Autowired 
	private Sid sid;
	/**
	 * <p>Title: saveVideo</p>
	 * <p>Description: 保存视频</p>
	 * @param videos
	 * @see com.Brewing.springboot.service.TpVideos.VideoService#saveVideo(com.Brewing.springboot.entity.TpVideos.TpVideos)
	 */ 
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public String saveVideo(TpVideos videos) {
		String id = sid.nextShort();
		videos.setId(id);
		
		TpNotice tpNotice = new TpNotice();
		tpNotice.setId(id);
		tpNotice.setTitle("您关注的更新拉...");
		tpNotice.setContent(videos.getVideoDesc());
		tpNotice.setCreateData(new Date());
		tpNotice.setCreateUseid(videos.getUserId());
		
		videosMapper.insertSelective(videos);
		tpNoticeMapper.insertSelective(tpNotice);
		return id;
	}
	/**
	 * <p>Title: updateVideo</p>
	 * <p>Description: </p>
	 */ 
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void updateVideo(String videoId, String coverPath) {
		// TODO Auto-generated method stub
		TpVideos videos = new TpVideos();
		videos.setId(videoId);
		videos.setCoverPath(coverPath);
		videosMapper.updateByPrimaryKeyIds(videos);
	}
	/**
	 * <p>Title: getAllVideos</p>
	 * <p>Description: 分页查询视频列表</p>
	 */ 
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public PagedResult getAllVideos(TpVideos videos,Integer isSaveRecord,Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		//保存热搜词
		String desc = videos.getVideoDesc();
		String userId = videos.getUserId();
		if(isSaveRecord!=null && isSaveRecord == 1) {
			TpSearchRecords record = new TpSearchRecords();
			String recordId = sid.nextShort();
			record.setId(recordId);
			record.setContent(desc);
			tpSearchRecordsMapper.insert(record);
		}
		
		PageHelper.startPage(page,pageSize);
		List<VideosVO> list = videosMapperCustom.queryAllVideos(desc,userId);
		
		PageInfo<VideosVO> pageList = new PageInfo<>(list);
		PagedResult pagedResult = new PagedResult();
		pagedResult.setPage(page);
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setRecords(pageList.getTotal());
		
		return pagedResult;
	}
	/**
	 * <p>Title: getHotWords</p>
	 * <p>Description:热搜词 </p>
	 */ 
	@Transactional(propagation=Propagation.SUPPORTS)
	@Override
	public List<String> getHotwords() {
		// TODO Auto-generated method stub
		return tpSearchRecordsMapper.getHotwords();
	}
	/**
	 * <p>Title: saveComment</p>
	 * <p>Description: 保存留言</p>
	 */ 
	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void saveComment(TpComments comment) {
		// TODO Auto-generated method stub
		String id = sid.nextShort();
		comment.setId(id);
		comment.setCreateTime(new Date());
		tpCommentsMapper.insert(comment);
	}
	/**
	 * <p>Title: getAllComments</p>
	 * <p>Description:获取全部评论 </p>
	 */ 
	@Override
	public PagedResult getAllComments(String videoId, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,pageSize);
		List<CommentsVO> list = tpCommentsMapperCustom.queryComments(videoId);
		for(CommentsVO commentsVO : list) {
			String timeAgo = TimeAgoUtils.format(commentsVO.getCreateTime());
			commentsVO.setTimeAgoStr(timeAgo);
		}
		PageInfo<CommentsVO> pageList = new PageInfo<>(list);
		
		PagedResult grid = new PagedResult();
		grid.setTotal(pageList.getPages());
		grid.setRows(list);
		grid.setPage(page);
		grid.setRecords(pageList.getTotal());
		return grid;
	}
	/**
	 * <p>Title: userLikeVideo</p>
	 * <p>Description:用户喜欢 </p>
	 */ 
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void userLikeVideo(String userId, String videoId, String videoCreaterId) {
		// TODO Auto-generated method stub
		// 1. 保存用户和视频的喜欢点赞关联关系表
		String likeId = sid.nextShort();
		TpUsersLikeVideos usersLikeVideos=new TpUsersLikeVideos();
		usersLikeVideos.setId(likeId);
		usersLikeVideos.setUserId(userId);
		usersLikeVideos.setVideoId(videoId);
		tpUsersLikeVideosMapper.insert(usersLikeVideos);
		// 2.视频喜欢数量累加
		videosMapperCustom.addVideoLikeCount(videoId);
		// 3.用户受喜欢数量增加
		usersMapper.addReceiveLikeCount(videoCreaterId);
	}
	/**
	 * <p>Title: userUnLikeVideo</p>
	 * <p>Description: </p>
	 */ 
	@Override
	public void userUnLikeVideo(String userId, String videoId, String videoCreaterId) {
		// TODO Auto-generated method stub
		TpUsersLikeVideosExample example = new TpUsersLikeVideosExample();
		Criteria criteria= example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andVideoIdEqualTo(videoId);
		tpUsersLikeVideosMapper.deleteByExample(example);
		videosMapperCustom.reduceVideoLikeCount(videoId);
		usersMapper.reduceReceiveLikeCount(videoCreaterId);
	}
	/**
	 * <p>Title: queryMyFollowVideos</p>
	 */ 
	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult queryMyFollowVideos(String userId, Integer page, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,pageSize);
		List<VideosVO> list = videosMapperCustom.queryMyFollowVideos(userId);
		PageInfo<VideosVO> pageList = new PageInfo<>(list);
		PagedResult pagedResult = new PagedResult();
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setPage(page);
		pagedResult.setRecords(pageList.getTotal());
		return pagedResult;
	}
	/**
	 * <p>Title: queryMyLikeVideos</p>
	 */ 
	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult queryMyLikeVideos(String userId, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page,pageSize);
		List<VideosVO> list = videosMapperCustom.queryMyLikeVideos(userId);
		
		PageInfo<VideosVO> pageList = new PageInfo<>(list);
		
		PagedResult pagedResult = new PagedResult();
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setPage(page);
		pagedResult.setRecords(pageList.getTotal());
		return pagedResult;
	}
	/**
	 * <p>Title: likeComment</p>
	 * <p>Description: </p>
	 * @param id
	 * @see com.Brewing.springboot.service.TpVideos.VideoService#likeComment(java.lang.String)
	 */ 
	@Override
	public void likeComment(String id) {
		// TODO Auto-generated method stub
		tpCommentsMapper.addLikeCount(id);
	}
}
